﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CSF.Utility.Mvc;
using CSF.Service.Accounts.Client;
using CSF.Security.Data;
using CSF.Utility.Misc;

namespace CSF.WebApp.Admin.Areas.Customers.Controllers
{
    public class CustomerUserRoleController : SecureController
    {
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 角色列表
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Index(bool isDisable = false)
        {

            UserRoleSvcClient svc = new UserRoleSvcClient();
            IEnumerable<UserRole> list = null;
            if (isDisable)
            {
                list = svc.GetDisable().Where(p => p.IsStaff == false);
            }
            else
            {
                list = svc.GetEnable().Where(p => p.IsStaff == false);
            }
            list = Search(list);
            return View(list);
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 角色列表搜索
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [NonAction]
        private IEnumerable<UserRole> Search(IEnumerable<UserRole> list)
        {
            var collection = Request.QueryString;

            var Code = collection["Code"];
            var name = collection["Name1"];

            if (!string.IsNullOrWhiteSpace(Code))
            {
                list = list.Where(p => Code.InStr(p.Code));
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                list = list.Where(p => name.InStr(new string[] { p.Name1, p.Name2, p.Name3 }));
            }
            return list;
        }
        [CsfAuthorize]
        public ActionResult Add()
        {

            return View();
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 新增角色
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Add(UserRole model, IEnumerable<int> checkedall)
        {
            if (ModelState.IsValid)
            {
                model.CreateBy = User.Identity.Name;
                UserRoleSvcClient svc = new UserRoleSvcClient();
                int roleid = svc.Insert(model);
                //保存权限
                if (checkedall != null && checkedall.Count() > 0)
                {
                    svc.SetPermission(roleid, checkedall, User.Identity.Name);
                }
                return RedirectToAction("Index");
            }
            return View(model);
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 编辑角色
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            UserRoleSvcClient svc = new UserRoleSvcClient();
            UserRole model = svc.GetOneById(id);
            return View(model);

        }

        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 编辑角色
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Edit(UserRole model, IEnumerable<int> checkedall)
        {
            if (ModelState.IsValid)
            {
                model.ModifyBy = User.Identity.Name;
                UserRoleSvcClient svc = new UserRoleSvcClient();
                svc.UpdateById(model, model.Id);
                if (checkedall != null && checkedall.Count() > 0)
                {
                    svc.SetPermission(model.Id, checkedall, User.Identity.Name);
                }
                return RedirectToAction("Index");
            }
            return View(model);
        }

        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 删除角色
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Delete(IEnumerable<int> id, string toUrl)
        {
            if (id != null && id.Count() > 0)
            {
                UserRoleSvcClient svc = new UserRoleSvcClient();
                svc.DeleteById(id);
            }
            return Redirect(toUrl);
        }

        [CsfAuthorize]
        public ActionResult RolePermissionControl(int id = 0)
        {
            PermissionSvcClient svcps = new PermissionSvcClient();
            UserRoleSvcClient svcur = new UserRoleSvcClient();
            var model = new Dictionary<int, int>();
            if (id > 0)
            {
                model = svcur.GetPermissions(id).ToDictionary(p => p);
            }

            ViewBag.Permissions = svcps.GetAll();
            return View(model);
        }
    }
}
